package cn.piesat.scanning.business.xz.service.clientimpl;

import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildFY3D_MERSI_L3_1000MServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //FY3D_L3级合成算法
        //GSE_NPP-_GBAL_L2_FY3D_MERSI_1000M_GLL_HHMM_20230731081500_20230731081500.TIFF
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        String[] s = fileName.split("_");
        Map<String, Object> params = new HashMap<>();
        if (taskPathParam.getParameter05().contains("AD")) {
            String date = s[9].substring(0, 8);
            String startTime = date + "000000";
            String endTime = date + "235959";
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    s[1].replace("-", ""), s[2], s[8], s[0], "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            String join = StrUtil.join(",", collect);
            params.put("primaryFile", join);
            params.put("CompositeType", taskPathParam.getParameter02());
            params.put("SpecialTopic", taskPathParam.getParameter03());
            params.put("SSLRID", taskPathParam.getParameter04());
            params.put("Code", taskPathParam.getParameter08());
            params.put("RegionId", taskPathParam.getParameter06());
            params.put("Period", taskPathParam.getParameter05());
            params.put("ShpFile", "/mnt/sfs/resource/shp/xizang_sheng_polygon.shp");
            params.put("XmlFile", null);
            params.put("Scale", null);
//                params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter11()));
            params.put("ExportPng", true);
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        } else if (taskPathParam.getParameter05().contains("TD")) {
            //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20230701000000_20230701235959.TIFF
            String startTime = s[9];
            if ("10".equals(startTime.substring(6, 8))) {
                startTime = startTime.substring(0, 6) + "01" + startTime.substring(8);
            } else if ("20".equals(startTime.substring(6, 8))) {
                startTime = startTime.substring(0, 6) + "11" + startTime.substring(8);
            } else if (Integer.parseInt(startTime.substring(6, 8)) >= 28) {
                startTime = startTime.substring(0, 6) + "21" + startTime.substring(8);
            }
            String endTime = s[10].replace(".TIFF", "");
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    taskPathParam.getParameter08(), taskPathParam.getParameter06(), s[8], taskPathParam.getParameter03(), "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            String join = StrUtil.join(",", collect);
            params.put("primaryFile", join);
            params.put("CompositeType", taskPathParam.getParameter02());
            params.put("SpecialTopic", taskPathParam.getParameter03());
            params.put("SSLRID", taskPathParam.getParameter04());
            params.put("Code", taskPathParam.getParameter08());
            params.put("RegionId", taskPathParam.getParameter06());
            params.put("Period", taskPathParam.getParameter05());
            params.put("XmlFile", null);
            params.put("Scale", null);
//                params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter11()));
            params.put("ExportPng", true);
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        } else if (taskPathParam.getParameter05().contains("AM")) {
            //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20230701000000_20230701235959.TIFF
            String productId = taskPathParam.getParameter08();
            String date = s[9].substring(0, 6);
            String startTime = date + "01000000";
            String endTime = date + "31235959";
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    taskPathParam.getParameter08(), taskPathParam.getParameter06(), s[8], taskPathParam.getParameter03(), "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            String join = StrUtil.join(",", collect);
            params.put("primaryFile", join);
            params.put("CompositeType", taskPathParam.getParameter02());
            params.put("SpecialTopic", taskPathParam.getParameter03());
            params.put("SSLRID", taskPathParam.getParameter04());
            params.put("Code", productId);
            params.put("RegionId", taskPathParam.getParameter06());
            params.put("Period", taskPathParam.getParameter05());
            params.put("XmlFile", null);
//                params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter11()));
            params.put("ExportPng", true);
            if ("GPP".equals(productId) || "NPP".equals(productId) || "NEP".equals(productId)) {
                int day = DateUtils.dayByMonth(DateUtils.parseStrToDate(s[9], "yyyyMMddHHmmss"));
                params.put("Scale", day);
            } else {
                params.put("Scale", null);
            }
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        } else if (taskPathParam.getParameter05().contains("AQ")) {
            String month = s[9].substring(4, 6); // 假设月份信息在日期字符串的第5位和第6位
            String quarter = null;
            switch (month) {
                case "03":
                case "04":
                case "05":
                    quarter = "1";
                    break;
                case "06":
                case "07":
                case "08":
                    quarter = "2";
                    break;
                case "09":
                case "10":
                case "11":
                    quarter = "3";
                    break;
                case "12":
                case "01":
                case "02":
                    quarter = "4";
                    break;
            }
            String year = s[9].substring(0, 4);
            String startTime = null, endTime = null;
            if ("1".equals(quarter)) {
                startTime = year + "0301000000";
                endTime = year + "0531235959";
            } else if ("2".equals(quarter)) {
                startTime = year + "0601000000";
                endTime = year + "0831235959";
            } else if ("3".equals(quarter)) {
                startTime = year + "0901000000";
                endTime = year + "1131235959";
            } else if ("4".equals(quarter)) {
                startTime = year + "12000000";
                int i = Integer.parseInt(year);
                int nextYear = i + 1;
                endTime = nextYear + "0231235959";
            }
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    taskPathParam.getParameter08(), taskPathParam.getParameter06(), s[8], taskPathParam.getParameter03(), "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            if (metadataDTOList.size() == 0) {
                algorithmParams.put("parameter01", null);
            } else {
                String join = StrUtil.join(",", collect);
                params.put("primaryFile", join);
                params.put("CompositeType", taskPathParam.getParameter02());
                params.put("SpecialTopic", taskPathParam.getParameter03());
                params.put("SSLRID", taskPathParam.getParameter04());
                params.put("Code", taskPathParam.getParameter08());
                params.put("RegionId", taskPathParam.getParameter06());
                params.put("Period", taskPathParam.getParameter05());
//                    params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter11()));
                params.put("ExportPng", true);
                params.put("Scale", taskPathParam.getParameter09());
                algorithmParams.put("parameter00", adsType);
                algorithmParams.put("parameter01", params);
            }
        } else if (taskPathParam.getParameter05().contains("AY")) {
            //BAS_NDVI_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20230701000000_20230701235959.TIFF
            String date = s[9].substring(0, 4);
            String startTime = date + "0101000000";
            String endTime = date + "1231235959";
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(startTime, endTime, s[3], s[4],
                    taskPathParam.getParameter08(), taskPathParam.getParameter06(), s[8], taskPathParam.getParameter03(), "TIFF");
            List<String> collect = metadataDTOList.stream().map(x -> scanningPath + File.separator + x.getFilePath()).collect(Collectors.toList());
            if (metadataDTOList.size() == 0) {
                algorithmParams.put("parameter01", null);
            } else {
                String join = StrUtil.join(",", collect);
                params.put("primaryFile", join);
                params.put("CompositeType", taskPathParam.getParameter02());
                params.put("SpecialTopic", taskPathParam.getParameter03());
                params.put("SSLRID", taskPathParam.getParameter04());
                params.put("Code", taskPathParam.getParameter08());
                params.put("RegionId", taskPathParam.getParameter06());
                params.put("Period", taskPathParam.getParameter05());
//                    params.put("ExportPng", Boolean.parseBoolean(taskPathParam.getParameter11()));
                params.put("ExportPng", true);
                params.put("Scale", taskPathParam.getParameter09());
                algorithmParams.put("parameter00", adsType);
                algorithmParams.put("parameter01", params);
            }

        }
        return algorithmParams;
    }
}
